package edu.ctbu.insurance.service.impl;

import com.github.pagehelper.PageHelper;
import edu.ctbu.insurance.bean.Customer;
import edu.ctbu.insurance.mapper.CustomerMapper;
import edu.ctbu.insurance.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author djl
 * @since 2024-06-28
 */
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    /**
     * 创建CustomerMapper实现类对象
     */
    @Autowired
    private CustomerMapper customerMapper;
    /**
     * 既可以查询所有客户信息，也可以按customer_name和customer_phone模糊查询客户信息
     * @param customerName 客户姓名
     * @param customerPhone 客户电话号码
     * @return 返回对应客户信息List
     */
    @Override
    public List<Customer> getAllCustomerLikeNameAndPhone(String customerName, String customerPhone) {
        return customerMapper.getAllCustomerLikeNameAndPhone(customerName,customerPhone);
    }

    /**
     * 既可以查询所有客户信息，也可以按customer_name和customer_phone模糊查询客户信息,并且实现分页展示
     * @param customerName 客户姓名
     * @param customerPhone 客户电话号码
     * @param pageNum 当前页
     * @param pageSize 每页大小
     * @return 返回对应客户信息List
     */
    @Override
    public List<Customer> getAllCustomerLikeNameAndPhoneAndPageHelper(String customerName, String customerPhone, int pageNum, int pageSize) {
        //pageHelper会拦截你要分页的sql语句  改造sql为分页sql
        //在该sql被调用之前   配置分页信息  由pageHelper生成分页语句
        PageHelper.startPage(pageNum,pageSize);
        return customerMapper.getAllCustomerLikeNameAndPhone(customerName, customerPhone);
    }

    /**
     * 根据customer_id删除，就是将对应delete_flag设置为0
     * @param customerId 客户ID
     * @return 成功返回true，失败返回false
     */
    @Override
    public boolean deleteCustomerById(String customerId) {
        int num = customerMapper.deleteCustomerById(customerId);
        if(num > 0){
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 查询所有软删除客户信息，并按删除时间降序
     * @return 所有软删除客户信息
     */
    @Override
    public List<Customer> getDeletedCustomer() {
        return customerMapper.getDeletedCustomer();
    }

    public Customer findCustomerByCustomerId(String customerId){
        return customerMapper.findCustomerByCustomerId(customerId);
    }
}
